﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using CoreDB;

namespace Core
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            imageView1.setImageView(cFviImageView1, true);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CoreUC.SetRegionForm setRegionForm = new CoreUC.SetRegionForm();
            setRegionForm.setImageView(cFviImageView1);
            setRegionForm.Show(this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CoreUC.LoginForm loginForm = new CoreUC.LoginForm("admin");
            loginForm.ShowDialog();
            string alertMessage = loginForm.IsAdmin ? "Admin" : "Not Admin";
            MessageBox.Show(alertMessage);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CoreUC.ChangePasswordForm changePassword = new CoreUC.ChangePasswordForm("admin");
            changePassword.ShowDialog();
            MessageBox.Show(changePassword.NewPassword);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            CoreUC.PatternRegistForm pp = new CoreUC.PatternRegistForm();
            pp.setImageView(cFviImageView1);
            pp.Show(this);
        }

        private void camera1_Load(object sender, EventArgs e)
        {
            camera1.setImageView(cFviImageView1, "CameraIni.xml", 0);
            camera1.eventPlay += new EventHandler(OnPlayCamera);
            camera1.eventPause += new EventHandler(OnPauseCamera);
        }

        private void OnPlayCamera(object sender, EventArgs e)
        {
            btnCameraSetting.Enabled = false;
        }

        private void OnPauseCamera(object sender, EventArgs e)
        {
            btnCameraSetting.Enabled = true;
        }

        private void btnCameraSetting_Click(object sender, EventArgs e)
        {
            camera1.releaseCamera();
            CoreUC.CameraSettingForm cameraForm = new CoreUC.CameraSettingForm("CameraIni.xml");
            cameraForm.ShowDialog();
            camera1.initCamera();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            FactoryConnection factoryConnection = new FactoryConnection();
            XMLConnection xmlConnection =
            factoryConnection.openXMLConnection("../../Database.xml");
            CoreDB.DataObject xmlObj = xmlConnection.loadDataObject();

            xmlObj.DataSet.Tables[2].Rows[0]["pattern_binary_level"] = 1127;
            xmlConnection.saveDataObject(xmlObj);

            Console.WriteLine("-------------------------------");
            Console.WriteLine("--------     XML   ------------");
            Console.WriteLine("-------------------------------");
            for (int i = 0; i < xmlObj.DataSet.Tables.Count; i++)
            {
                Console.WriteLine(xmlObj.DataSet.Tables[i].TableName);
                foreach (DataRow row in xmlObj.DataSet.Tables[i].Rows)
                {
                    foreach (DataColumn col in xmlObj.DataSet.Tables[i].Columns)
                    {
                        Console.WriteLine(col.ColumnName + " : " + row[col.ColumnName]);
                    }
                }
                Console.WriteLine("-------------------------------");
            }

            //SqliteConnection sqliteConnection =
            //    factoryConnection.openSqliteConnection("../../test.db");
            //CoreDB.DataObject sqliteObj = sqliteConnection.loadDataObject();

            //sqliteObj.DataSet.Tables[0].Rows[0]["seq"] = 111;
            //sqliteConnection.saveDataObject(sqliteObj);

            //Console.WriteLine("");
            //Console.WriteLine("-------------------------------");
            //Console.WriteLine("-------     SQLite   -----------");
            //Console.WriteLine("-------------------------------");
            //for (int i = 0; i < sqliteObj.DataSet.Tables.Count; i++)
            //{
            //    Console.WriteLine(sqliteObj.DataSet.Tables[i].TableName);
            //    foreach (DataRow row in sqliteObj.DataSet.Tables[i].Rows)
            //    {
            //        foreach (DataColumn col in sqliteObj.DataSet.Tables[i].Columns)
            //        {
            //            Console.WriteLine(col.ColumnName + " : " + row[col.ColumnName]);
            //        }
            //    }
            //    Console.WriteLine("-------------------------------");
            //}
        }
    }
}
